/**
 * <p> 获取加载图片地址 </p>
 * examples: bg/bg.png
 * @param imgPath: string
 */
export const requireImg = (imgPath) => {
  return new URL(`../assets/images/${imgPath}`, import.meta.url).href
}

//将数字保留两位小数
export const fixedValue = (value, fixNum = 2, defaultValue = '--') => {
  if (value === undefined || value === null) {
    return defaultValue
  }
  return (Math.round(value * Math.pow(10, fixNum)) / Math.pow(10, fixNum)).toFixed(fixNum)
}

//计算数组中某一项占所有项的百分比
export const calculatePercentage = (arr, index) => {
  if (index < 0 || index >= arr.length) {
    throw new Error('Index out of bounds')
  }

  const total = arr.reduce((sum, value) => sum + value, 0)
  const valueAtIndex = arr[index]

  // 处理总和为 0 的情况，避免除以 0
  if (total === 0) {
    return 0
  }

  const percentage = (valueAtIndex / total) * 100
  return fixedValue(percentage) + '%'
}

//时间补零
export const toDou = (n) => {
  return parseInt(n) < 10 ? '0' + n : n
}

// 节流
export const throttle = (fn, interval) => {
  let lastTime = 0
  return function () {
    const now = new Date().getTime()
    const remainTime = interval - (now - lastTime)
    if (remainTime <= 0) {
      fn()
      lastTime = now
    }
  }
}

//要将数字格式化为特定的字符串格式 如将20248 、202410改成1024-08、2014-10
export const formatDate = (num) => {
  // 将数字转换为字符串
  const str = num.toString()

  // 提取年份和月份
  let year, month

  if (str.length === 5) {
    // 处理 20248
    year = str.substring(0, 4) // 前四位为年份
    month = str.substring(4, 5).padStart(2, '0') // 后一位为月份，确保为两位数
  } else if (str.length === 6) {
    // 处理 202410
    year = str.substring(0, 4) // 前四位为年份
    month = str.substring(4) // 后两位为月份
  } else {
    return 'Invalid input' // 处理无效输入
  }

  // 格式化输出
  return `${year}-${month}`
}

//将数组按照一定长度分页
export const paginateArray = (array, pageSize) => {
  return array.reduce((result, item, index) => {
    const pageIndex = Math.floor(index / pageSize)
    if (!result[pageIndex]) {
      result[pageIndex] = []
    }
    result[pageIndex].push(item)
    return result
  }, [])
}

//将时间戳转成日期
export function formatTimestamp(timestamp) {
  const date = new Date(timestamp)

  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

export function createCircle(center, radius, startAngle = 0, endAngle = 360) {
  const points = []
  for (let i = startAngle; i < endAngle; i++) {
    const radians = (i * Math.PI) / 180
    const x = Math.sin(radians) * radius
    const y = Math.cos(radians) * radius
    points.push([center[0] + x, center[1] + y, center[2] || 0])
  }
  return points
}

export function calculateCentroid(vertices) {
  let xSum = 0
  let ySum = 0
  const n = vertices.length
  for (let i = 0; i < n; i++) {
    xSum += vertices[i][0]
    ySum += vertices[i][1]
  }
  return [xSum / n, ySum / n]
} // 示例：四边形的顶点 const vertices = [[1, 1], [4, 1], [4, 4], [1, 4]];
//const centroid = calculateCentroid(vertices); console.log(`重心坐标: (${centroid[0]}, ${centroid[1]})`);
// 通过角度换算风向
export const getWindDirection = (angle, flag) => {
  let arr = ['N', 'NE', 'E', 'ES', 'S', 'WS', 'W', 'WN']
  if (flag) {
    arr = ['北风', '东北风', '东风', '东南风', '南风', '西南风', '西风', '西北风']
  }
  angle = ((angle % 360) + 360) % 360 // 确保角度在0到359之间
  if (angle >= 337.5 || angle < 22.5)
    return arr[0] // 北风
  else if (angle >= 22.5 && angle < 67.5)
    return arr[1] // 东北风
  else if (angle >= 67.5 && angle < 112.5)
    return arr[2] // 东风
  else if (angle >= 112.5 && angle < 157.5)
    return arr[3] // 东南风
  else if (angle >= 157.5 && angle < 202.5)
    return arr[4] // 南风
  else if (angle >= 202.5 && angle < 247.5)
    return arr[5] // 西南风
  else if (angle >= 247.5 && angle < 292.5)
    return arr[6] // 西风
  else if (angle >= 292.5 && angle < 337.5) return arr[7] // 西北风
}
